<?php
define('MOD_FLAG', 'howcan');
define('MOD_ROOT', MUDDER_MODULE . MOD_FLAG . DS);

// echo MUDDER_CACHE . MOD_FLAG.'_config.php';

if(!$_G['mod'] = read_cache(MUDDER_CACHE . MOD_FLAG.'_config.php')) 
{
    if(isset($_G['modules'][MOD_FLAG])) {
        $C =& $_G['loader']->model('config');
        $C->write_cache(MOD_FLAG);
	MOD_ROOT . 'inc' . DS . 'cache.php';
        @include MOD_ROOT . 'inc' . DS . 'cache.php';
        show_error('global_cache_module_succeed');
    }
    if(empty($_G['mod'])) {
        redirect('global_module_not_install');
    }
}

// errMessage(USER_NOT_LOGIN, '用户未登录');

if(!$_G['modules'][MOD_FLAG]) redirect('global_module_disable');
$_G['mod'] = array_merge($_G['mod'], $_G['modules'][MOD_FLAG]);
$MOD =& $_G['mod'];

$mobile = $_G['loader']->model('howcan:user');
$mobile->auto_login();

if(!defined('IN_ADMIN')) {
    $acts = array('user','item','item_detail','member','list','login');
    if(!in_array($_GET['act'], $acts)) $_GET['act'] = 'index';

    include MOD_ROOT . $_GET['act'] . '.php';
}

function write_bin($name, &$data, $model=NULL, $type='return', $custom_dir = '') {
    $cachedir = $custom_dir ? MUDDER_ROOT . ($custom_dir.DS) : MUDDER_CACHE;
    if(!$model) $model = 'modoer';
    $filename = $type == 'bin' ? ($name . '.php') : ($model . '_' . $name . '.php');

    if(!is_dir($cachedir)) {
        if(@mkdir($cachedir, 0777)) {
            show_error(sprintf(lang('global_mkdir_no_access'), str_replace(MUDDER_ROOT,'./',$cachedir)));
        }
    }

	if ($type == 'return') {
        $content = "<?php\r\n!defined('IN_MUDDER') && exit('Access Denied');\r\nreturn " . $data . "; \r\n?>";
    } else {
        $content = _errMessage(0,$data);
    }

    $file = $cachedir . $filename;
    if(!$x = file_put_contents($file, $content)) {
        show_error(lang('global_file_not_exist', str_replace(MUDDER_ROOT, '.'.DS, $file)));
    }
    @chmod($file, 0777);
}

function _format($data, $requestCharset = '',$format = 'xml') {
		if (!empty($requestCharset)) {
			$data = pwConvert($data, $requestCharset, $_G['charset']);
		}
		$data = _escapeStrDecodeArray($data);
print_r($data);
		//		$charset = empty($requestCharset) ? $_G['charset'] : $requestCharset;
		if ($format == 'xml') {
			$array2xml = new Array2Xml($data, $charset);
			return $array2xml->getXml();
		}
		return json_encode($data);
	}

	function _errMessage($errCode, $errMesssage) {
		return json_encode(array('errCode' => $errCode, 'errMessage' => $errMesssage));
	}
	
	function errMessage($errCode, $errMesssage,$requestCharset = 'utf8',$format = 'json') {
		echo json_encode(array('errCode' => $errCode, 'errMessage' => $errMesssage));
		//, $requestCharset,$format);
		exit;
	}
	
		function _escapeStrDecodeArray($param) {
		if (is_array($param)) {
			foreach ($param as $key => $value) {
				$param[$key] = _escapeStrDecodeArray($value);
			}
		} else {
			$param = _escapeStrDecode($param);
		}
		return $param;
	}
	/**
	 * 字符转换
	 * @param $string
	 * @return string
	 */
	function _escapeStrDecode($string) {
		$string = str_replace('&lt;', '<', $string);
		$string = str_replace('&gt;', "%3E", $string);
		$string = str_replace(array('&quot;', '&#39;', '    ', '&nbsp;&nbsp;', '&#160;'), array('"', "'", "\t", '  ', ' '), $string);
		return $string;
	}

	
class Array2Xml {
    var $xml;
	
    function Array2Xml($array, $encoding = 'utf-8') {
		$this->xml = '<?xml version="1.0" encoding="' . $encoding . '"?>';
        $this->xml .= $this->_array2xml($array);
    }
	
    function getXml() {
        return $this->xml;
    }
		
    function _array2xml($array){
        $xmlStr = '';
		foreach($array as $key => $val) {
			if (preg_match("/^\d.*/",$key)){
			    $key = 'item';
			}
			
            $xmlStr .= "<$key>";
            $xmlStr .= is_array($val) ? $this->_array2xml($val) : $this->_xmlEscape($val);
            $xmlStr.="</$key>";
        }
        return $xmlStr;
    }
	
	function _xmlEscape($str){
		$search = array('&', '<', '>', '"', '\'');
        $replace = array('&amp;', '&lt;', '&gt;', '&quot;', '&apos;');
        return str_replace($search, $replace, $str);
	}
}

/**
 * 编码转换
 *
 * @uses Chinese
 * @param string $str 内容字符串
 * @param string $toEncoding 转为新编码
 * @param string $fromEncoding 原编码
 * @param bool $ifMb 是否使用mb函数
 * @return string
 */
function pwConvert($str, $toEncoding, $fromEncoding, $ifMb = true) {
	if (strtolower($toEncoding) == strtolower($fromEncoding)) {return $str;}
	is_object($str) && $str = get_object_vars($str);//fixed: object can't convert, by alacner 2010/09/15
	if (is_array($str)) {
		foreach ($str as $key => $value) {
			is_object($value) && $value = get_object_vars($value);
			$str[$key] = pwConvert($value, $toEncoding, $fromEncoding, $ifMb);
		}
		return $str;
	} else {
		if (function_exists('mb_convert_encoding') && $ifMb) {
			return mb_convert_encoding($str, $toEncoding, $fromEncoding);
		} else {
			static $sConvertor = null;
			!$toEncoding && $toEncoding = 'GBK';
			!$fromEncoding && $fromEncoding = 'GBK';
			if (!isset($sConvertor) && !is_object($sConvertor)) {
				L::loadClass('Chinese', 'utility/lang', false);
				$sConvertor = new Chinese();
			}
			return $sConvertor->Convert($str, $fromEncoding, $toEncoding, !$ifMb);
		}
	}
}
?>